/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.entity.bo.BalanceBo;
import com.yiqi.lottery.entity.bo.PayStautsBo;
import com.yiqi.lottery.entity.bo.RechargeBo;
import com.yiqi.lottery.entity.bo.RechargeCallBackBo;
import com.yiqi.lottery.feature.mine.contract.BanlanceContract;
import com.yiqi.lottery.feature.mine.contract.RechargeContract;
import com.yiqi.lottery.feature.mine.dependencies.recharge.DaggerRechargePresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class RechargePresenter implements RechargeContract.Presenter {

    private RechargeContract.View mView;

    @Inject

    RechargeContract.Model mModel;


    public RechargePresenter(RechargeContract.View mView) {
        this.mView = mView;
        DaggerRechargePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void doRecharge(double prize, int type) {
        if (prize ==0) {
            T.showShort("请选择充值金额！");
            return;
        }
        RechargeBo rechargeBo = new RechargeBo();
        rechargeBo.setValue(prize);
        rechargeBo.setPay_type(type);
        mModel.toRecharge(rechargeBo).compose(mView.bindToLife()).subscribe(new Observer<RechargeBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(RechargeBo rechargeBo) {
                mView.dismissLoading();
                mView.showMyRechargeSuccess(rechargeBo);

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showMyRechargeError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void toWithdraw(double prize) {
        if (prize ==0) {
            T.showShort("请填写充值金额！");
            return;
        }
        RechargeBo rechargeBo = new RechargeBo(prize);
        mModel.toWithdraw(rechargeBo).compose(mView.bindToLife()).subscribe(new Observer<RechargeBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(RechargeBo rechargeBo) {
                mView.dismissLoading();
                mView.showWithdrawSuccess(rechargeBo);

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showMyRechargeError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void toYLPay(String id) {
        if (id==null){
            return;
        }

        mModel.toYLPay(id).compose(mView.bindToLife()).subscribe(new Observer<PayStautsBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PayStautsBo rechargeBo) {
                mView.dismissLoading();
                mView.showYLPaySuccess(rechargeBo);

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showMyRechargeError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
    @Override
    public void doGetUserInfo() {


        mModel.getUsertInfo().compose(mView.bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
//                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
//                mView.dismissLoading();
                mView.showUserInfoSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
//                mView.dismissLoading();
                T.showShort(e.getMessage());
            }

            @Override
            public void onComplete() {
//                mView.dismissLoading();
            }
        });
    }

}
